define(function(){
	var TypeTreePanel = Ext.extend(Ext.tree.TreePanel, {
		postPanel: null, //FQA内容列表面板
        collapsible: false,
        border: false,
        width: 200,
        margins:'0 0 3 3',
		useArrows: true,
        autoScroll: true,
        split: true,
        initComponent: function(){
        	TypeTreePanel.superclass.initComponent.call(Ext.apply(this, {
	        	tbar: au.permit([
	        	    {xtype:'label', text:'FQA类型', style:'font-weight:bold;margin-right:5px;'},
	        	    {permitId:'misc.fqa.addType', handler:this.addType, scope:this, iconCls:'icon-add', tooltip:'添加类型'},
	        	    {permitId:'misc.fqa.modifyType', handler:this.modifyType, scope:this, iconCls:'icon-edit', tooltip:'修改类型'},
	        	    {permitId:'misc.fqa.removeType', handler:this.removeType, scope:this, iconCls:'icon-delete', tooltip:'删除类型'},
	        	    '-',
	        	    {handler: this.refresh, scope: this, iconCls:'icon-refresh'}
	        	]),
	            dataUrl: 'misc/fqa/getTypeChildren.do',
	            root: new Ext.tree.AsyncTreeNode({
	            	id: '0', 
	            	text: '全部类型',
	            	expanded: true, 
	            	listeners:{
		            	expand: function(node){ //默认展开第一层节点
		            		node.eachChild(function(n){n.expand();});
		            		node.select();
		            		node.ownerTree.onNodeClick(node);
		        		}
	            	}
	            }),
	            rootVisible: true,
	        	listeners:{
	        		click: this.onNodeClick
	        	}
        	}));
        },
        onNodeClick: function(node){
        	this.postPanel.setFqaType(node ? node.attributes : null);
        	this.postPanel.refresh();
        },
        getCurNode: function(){
        	return this.getSelectionModel().getSelectedNode();
        },
        refresh: function(){
        	this.getLoader().load(this.root, function(n){n.expand();});
        },
        selectType: function(typePath){
        	this.selectPath(typePath, 'id', function(success, node){
        		node.ownerTree.onNodeClick(node);
        	});
        },
        addType: function(){
        	var parent = this.getSelectionModel().getSelectedNode();
        	if (parent.isLeaf()) parent = parent.parentNode;
			use('misc.fqa.TypeWindow', function(Win){
				new Win({
					parentType: parent || this.root,
					callback: function(data, parentPath){
						this.selectPath(parentPath, 'id', function(success, node){
							var tree = node.getOwnerTree();
							tree.getLoader().load(node, function(){
								tree.selectType(parentPath + '/' + data.id);
							});
						});
					},
					cbScope: this
				}).show();
			}, this);
        },
        modifyType: function(){
        	var node = this.getSelectionModel().getSelectedNode();
			if (!node || node == this.root){
				cu.hint('请选择要修改的FQA类型');
				return;
			}
			use('misc.fqa.TypeWindow', function(Win){
				new Win({
					modifiedId: node.id,
					parentType: parent || this.root,
					callback: function(data, parentPath){
						if (node.parentNode.getPath() != parentNodePath){ //修改了父节点，原来的节点要删除
							node.remove();
						}
						this.selectPath(parentPath, 'id', function(success, n){
							var tree = n.getOwnerTree();
							tree.getLoader().load(n, function(){
								tree.selectType(parentPath + '/' + data.id);
							});
						});
					},
					cbScope: this
				}).show();
			}, this);
        },
        removeType: function(){
        	var node = this.getSelectionModel().getSelectedNode();
			if (!node){
				cu.hint('请选择要删除的FQA类型');
				return;
			}
			if (node == root){
				cu.hint('不能删除根节点');
				return;
			}
			cu.confirm('确定要删除这个类型吗？', function(){
				cu.post({
					url: 'misc/fqa/deleteType.do', 
					params: {id: node.id},
					success: function(){
						node.remove();
						this.onNodeClick(null);
					}, 
					scope: this
				});
			}, this);
        }
	});
	return TypeTreePanel;
});